/**
 * 
 */
package org.braketester.view;

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.braketester.entry.FramesThread;
import org.braketester.model.LTR212DeviceSingletonCeeper;
import org.braketester.model.RawData;
import org.braketester.model.RawDataReader;
import org.braketester.model.RequestThread;
import org.braketester.model.filters.RunnableSeriesAddValueFilter;
import org.extdev.libraries.ltr.LTRException;
import org.jfree.data.xy.XYSeries;

/**
 * @author LAP
 *
 */
public class CreateRawDataFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private JPanel content;
	private JLabel name;
	private JTextField nameTextField;
	private JButton apply;
	private JButton cancel;
	
	private JPanel buttonsPanel;
	
	public CreateRawDataFrame() {
		super("New measurment");
		createGUI();
	}

	private void createGUI() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		content = new JPanel(gridBagLayout);
		name = new JLabel("name");
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.anchor = GridBagConstraints.WEST;
		constraints.insets.top = 2;
		constraints.gridx = 0;
		constraints.gridy = 0;
		content.add(name, constraints);
		
		constraints.gridx++;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.insets.left = 5;
		constraints.weightx = 1;
		nameTextField = new JTextField("Noname");
		content.add(nameTextField, constraints);
		
		constraints.gridy++;
		constraints.gridx = 0;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.insets.left = 0;
		constraints.weighty = 1;
		constraints.gridwidth = 2;
		buttonsPanel = new JPanel();
		apply = new JButton("Apply");
		apply.addActionListener(new CreateRawDataFrameApplyButtonListener());
		cancel = new JButton("Cancel");
		cancel.addActionListener(new CreateRawDataFrameCancelButtonListener());
		buttonsPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.LINE_AXIS));
		buttonsPanel.add(cancel);
		buttonsPanel.add(Box.createHorizontalStrut(5));
		buttonsPanel.add(apply);

		content.add(buttonsPanel, constraints);
		setContentPane(content);
		setMinimumSize(new Dimension(200, 100));
		setResizable(false);
		pack();
	}
	
	private class CreateRawDataFrameCancelButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			setVisible(false);
		}
	}
	
	private class CreateRawDataFrameApplyButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			/*RawData data = new RawData(1);
			data.setName(nameTextField.getText());
			RequestThread requestThread = new RequestThread(LTR212DeviceSingletonCeeper.getInstance().getDevice(), 1);
			XYSeries series = new XYSeries("");
			RunnableSeriesAddValueFilter filter = new RunnableSeriesAddValueFilter(series);
			RawDataReader dataReader = new RawDataReader(filter, requestThread, 1);
			FramesThread.getInstance().getMainFrame().addMeasurement(data).addXYSeries(series);
			try {
				LTR212DeviceSingletonCeeper.getInstance().getDevice().startDataReading();
			} catch (LTRException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			requestThread.start();
			dataReader.start();
			filter.start();
			setVisible(false);*/
		}
	}
}
